home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE11 / FILES / RWEDITU.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  2.5 KB  |  118 lines

  1. unit Rweditu;
  2.  
  3. interface
  4.  
  5. uses
  6.   StdCtrls;
  7.  
  8. procedure AssignRWEdit(var F: TextFile; E: TEdit);
  9.  
  10. implementation
  11.  
  12. uses
  13.   Forms, SysUtils;
  14.  
  15. const
  16. {$ifdef Win32}
  17.   FillerMax = 32;
  18. {$else}
  19.   FillerMax = 16;
  20. {$endif}
  21.  
  22. type
  23.   TUserData = packed record
  24.     Edit: TEdit;
  25.     Filler: array[SizeOf(TEdit)+1..FillerMax] of Byte;
  26.   end;
  27.   {TUserData = packed record
  28.     case Byte of
  29.       1: (
  30.         Edit: TEdit);
  31.       2: (
  32.         Filler: array[1..FillerMax] of Byte);
  33.   end;}
  34.  
  35. function RWEditOpen(var F: TTextRec): Integer; far; forward;
  36. function RWEditInput(var F: TTextRec): Integer; far; forward;
  37. function RWEditOutput(var F: TTextRec): Integer; far; forward;
  38. function RWEditClose(var F: TTextRec): Integer; far; forward;
  39.  
  40. procedure AssignRWEdit(var F: TextFile; E: TEdit);
  41. begin
  42.   { Set up text file variable }
  43.   with TTextRec(F) do
  44.   begin
  45.     Handle := $FFFF;
  46.     OpenFunc := @RWEditOpen;
  47.     Mode := fmClosed;
  48.     BufSize := SizeOf(Buffer);
  49.     BufPtr := @Buffer;
  50.     Name[0] := #0;
  51.     { Set up edit control - store it in the text file variable }
  52.     TUserData(UserData).Edit := E;
  53.   end;
  54. end;
  55.  
  56. function RWEditOpen(var F: TTextRec): Integer;
  57. begin
  58.   Result := 0;
  59.   with F do
  60.   begin
  61.     if Mode = fmInput then
  62.     begin
  63.       InOutFunc := @RWEditInput;
  64.       FlushFunc := nil;
  65.     end
  66.     else
  67.     begin
  68.       Mode := fmOutput;
  69.       InOutFunc := @RWEditOutput;
  70.       FlushFunc := @RWEditOutput;
  71.     end;
  72.     CloseFunc := @RWEditClose;
  73.   end;
  74. end;
  75.  
  76. function RWEditInput(var F: TTextRec): Integer;
  77. begin
  78.   Result := 0;
  79.   with F, TUserData(UserData).Edit do
  80.   begin
  81.     BufPos := 0;
  82.     BufEnd := GetTextBuf(PChar(BufPtr), BufSize);
  83.     { Pop a carriage return line feed combo in }
  84.     StrCat(PChar(BufPtr), #13#10);
  85.     Inc(BufEnd, 2);
  86.     { Clear the edit }
  87.     Text := '';
  88.   end;
  89. end;
  90.  
  91. function RWEditOutput(var F: TTextRec): Integer;
  92. var
  93.   { Temporary PChar holder }
  94.   Buf: packed array[0..255] of Char;
  95. begin
  96.   Result := 0;
  97.   { This gets called when a Delphi 2 app shuts, in closing Output }
  98.   { Since it refers to the edit which won't exist, don't run it }
  99.   if not Application.Terminated then
  100.     with F, TUserData(UserData).Edit do
  101.       if BufPos <> 0 then
  102.       begin
  103.         { Get PChar with BufPos characters in }
  104.         StrLCopy(Buf, PChar(BufPtr), BufPos);
  105.         { Put that in the edit }
  106.         SetTextBuf(Buf);
  107.         { Reset BufPos }
  108.         BufPos := 0;
  109.       end;
  110. end;
  111.  
  112. function RWEditClose(var F: TTextRec): Integer;
  113. begin
  114.   Result := 0;
  115. end;
  116.  
  117. end.
  118.